/* Copyright (C) 2011 rccloud.com All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rccloud.gwtproxyc.server;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.rccloud.gwtproxyc.shared.ProxyConverter;

/**
 * The converter for Google App Engine
 * {@link com.google.appengine.api.datastore.Key Key} field. It will convert
 * between {@link com.google.appengine.api.datastore.Key Key} type and string
 * type.
 */
public class AppEngineKeyConverter implements ProxyConverter {

	@Override
	public Object convertToProxy(Class<?> proxyFieldClass, Object object) {
		if (object instanceof Key) {
			return KeyFactory.keyToString((Key) object);
		}
		return object;
	}

	@Override
	public Object convertFromProxy(Object object) {
		if (object instanceof String) {
			return KeyFactory.stringToKey((String) object);
		}
		return object;
	}
}
